(function ($) {
        $.uuid = function() {
            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
                return v.toString(16);
            });
        };

        var newDate = new Date();
        var year = newDate.getFullYear();
        var month = newDate.getMonth() + 1;

        if (month < 10) {
            month = "0" + month;
        }

        $(document).ready(function () {

            var uploadPath = $("#uploadUrl").val();

            var storeUrl = $("#storeUrl").val();

            $(".upload-image").click(function (e) {
                e.preventDefault();
                if (FormData != null) {

                    var form = new FormData();
                    if ($("#imageFile")[0].files.length == 0) {
                        $("#imageFileSpan").html("请选择图片");
                        return;
                    }
                    var fileObject = $("#imageFile")[0].files[0];

                    form.append("file", fileObject);
                    form.append("token", $("#qiniuUploadToken").val());

                    var suffix = $("#imageFile").val().substr($("#imageFile").val().lastIndexOf("."));
                    var fileFullName = 'images' + '-' + year + '-' + month + '-' + $.uuid() + suffix;
                    form.append("key", fileFullName);

                    var xhr = new XMLHttpRequest();
                    if (window.XMLHttpRequest)
                    {// code for IE7, Firefox, Opera, etc.
                        xhr = new XMLHttpRequest();
                    }
                    xhr.open("post", uploadPath, true);
                    xhr.onload = function (e) {
                        var data = JSON.parse(e.currentTarget.response);
                        var imageInfo = {'fileUrl': storeUrl + data.key, 'fileId': data.key};
                        $("#imageUploadContainer").append('<img src="' + imageInfo.fileUrl + '" id="' + imageInfo.fileId + '" width="250px" height="200px">'+'&nbsp;');
                        $("#upload_file_container").append('<input type="hidden" value ="' + imageInfo.fileId + '"name="imgFileIds" id="imgFileIds">');
                        $("#imageFile").val("");
                        $("#imageFileSpan").html("");
                    };
                    xhr.send(form);
                } else {
                    alert("不支持Ajax上传，请用iframe实现");
                }
            });
        });
    }
    (jQuery)
)
;
